$(function () {
    let id = sessionStorage.getItem("buildId");
    findDetailsById(id);
});

/***
 * 根据新闻id查询详细数据
 */
function findDetailsById(id) {
    let result = myAjax("/admin/product/findByDetailsId", {id: id}, "get");
    setBuildDetailsData(result.list);
    console.log(result.list, "--------");
}

/**
 * 赋值
 * @param data
 */
function setBuildDetailsData(data) {
    //如果data的长度为1，说明数据只有一条，
    if (data.length === 1) {
        $("#lastPage").html("无");
        $("#nextPage").html("无");
        $("#detailImg").attr("src", URI+'/propertyimg'+data.img);
        $("#newsTitle").html(data.name);
        $("#newsPublishTime").html("发布时间：" + data.createTime);
        $("#brief").html(data.description);
        $("#detailContent").html(data.description);

    }
    //如果data的长度为2，说明数据有两条，不知道是上一条还是下一条
    if (data.length === 2) {
        //当 i==0时，说明查询到的数据没有上一条数据
        let id = sessionStorage.getItem("buildId");
        if (data[0].id == id) {
            $("#lastPage").html("无");
            $("#nextPage").html(data[1].name);
            $("#detailImg").attr("src", URI+'/propertyimg'+data[0].img);
            $("#newsTitle").html(data[0].name);
            $("#newsPublishTime").html("发布时间：" + data[0].createTime);
            $("#brief").html(data[0].description);
            $("#detailContent").html(data[0].description);
        }
        if (data[1].id == id) {
            $("#lastPage").html(data[0].name);
            $("#nextPage").html("无");
            $("#detailImg").attr("src", URI+'/propertyimg'+data[1].img);
            $("#newsTitle").html(data[1].name);
            $("#newsPublishTime").html("发布时间：" + data[1].createTime);
            $("#brief").html(data[1].description);
            $("#detailContent").html(data[1].description);
        }
    }
    //如果data的长度为3，说明数据有三条，该数据有上一条也有下一条
    if (data.length == 3) {
        for (let i = 0; i < data.length; i++) {
            //选中中间的数据赋值
            if (i == 1) {
                $("#detailImg").attr("src", URI+'/propertyimg'+data[i].img);
                $("#newsTitle").html(data[i].name);
                $("#newsPublishTime").html("发布时间：" + data[i].createTime);
                $("#brief").html(data[i].description);
                $("#detailContent").html(data[i].description);
                $("#lastPage").html( data[i - 1].name);
                $("#nextPage").html( data[i + 1].name);
            }
        }
    }
}

/*上一篇*/
function toLastPage() {
    let oldId = parseInt(sessionStorage.getItem("buildId"));
    if ($("#lastPage").html() === "无") {
        alert("已经是第一篇了^_^");
        // findDetailsById(id);
    } else {
        let buildId = oldId - 1;
        sessionStorage.setItem("buildId", buildId);
        findDetailsById(sessionStorage.getItem("buildId"));
    }
}

/**
 * 下一篇
 */
function toNextPage() {
    let oldId = parseInt(sessionStorage.getItem("buildId"));
    // console.log(typeof(oldId),"--------")
    if ($("#nextPage").html() === "无") {
        alert("已经是最后一篇了^_^");
        // findDetailsById(id);
    } else {
        let buildId = oldId + 1;
        sessionStorage.setItem("buildId", buildId);
        findDetailsById(sessionStorage.getItem("buildId"));
    }

}
